import axios from "axios";
import JSONBig from 'json-bigint';
const instance = axios.create({
  //baseURL: "http://localhost:8010",
  baseURL: "http://104.160.47.201:8010",
    transformResponse: [
        data => {
            data=JSONBig.parse(data);
            return data;
        }
    ]
});
instance.interceptors.request.use(
  (config) => {
    if (!config.url.includes("/user/login")) {
      config.headers["token"] = sessionStorage.getItem("token");
    }
    return config;
  },
  (error) => {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);

instance.interceptors.response.use(
  (res) => {
    if (res?.data) {
      return Promise.resolve(res.data);
    } else {
      // token过期，退出登录
      if (res?.headers?.tokenresult === "false") {
        sessionStorage.setItem("token", "");
        location.href = "#/login";
      } else {
        Promise.reject(res?.data || '错误');
      }
    }
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default instance;
